package com.ydd.moudle.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.ydd.R;
import com.ydd.base.BaseActivity;
import com.ydd.util.StatusBarColorUtil;

public class HomeActivity extends BaseActivity implements RadioButton.OnCheckedChangeListener {
    private static final String TAG_HOME = "home";
    private static final String TAG_MESSAGE = "message";
    private static final String TAG_ME = "me";

    private FragmentManager mFragmentManager;
    private Fragment mCurrentFragment;
    private Fragment mHomeFragment;
    private Fragment mMessageFragment;
    private Fragment mMeFragment;

    private RadioButton mHomeBtn;
    private RadioButton mMessageBtn;
    private RadioButton mMeBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_layout);
        initView();
        initFragment(savedInstanceState);
    }

    private void initView() {
        mFragmentManager = getSupportFragmentManager();
        mHomeBtn = (RadioButton) findViewById(R.id.tab_home_btn);
        mMessageBtn = (RadioButton) findViewById(R.id.tab_message_btn);
        mMeBtn = (RadioButton) findViewById(R.id.tab_me_btn);

        mHomeBtn.setOnCheckedChangeListener(this);
        mMessageBtn.setOnCheckedChangeListener(this);
        mMeBtn.setOnCheckedChangeListener(this);

    }

    private void initFragment(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mCurrentFragment = mHomeFragment = mFragmentManager.findFragmentByTag(TAG_HOME);
            mMessageFragment = mFragmentManager.findFragmentByTag(TAG_MESSAGE);
            mMeFragment = mFragmentManager.findFragmentByTag(TAG_ME);
            if (mHomeFragment == null) {
                mCurrentFragment = mHomeFragment = new HomeFragment();
            }
            if (mMessageFragment == null) {
                mMessageFragment = new MeFragment();
            }
            if (mMeFragment == null) {
                mMeFragment = new MeFragment();
            }
        } else {
            mCurrentFragment = mHomeFragment = new HomeFragment();
            mMessageFragment = new MessageFragment();
            mMeFragment = new MeFragment();
            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragments, mHomeFragment, TAG_HOME).commit();
        }
    }

    private void changeFragment(Fragment fragment, String fragmentTag) {

        if (fragment == mCurrentFragment) {
            return;
        }
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        if (!fragment.isAdded()) {
            fragmentTransaction.hide(mCurrentFragment).add(R.id.fragments, fragment, fragmentTag)
                    .show(fragment).commit();
        } else {
            fragmentTransaction.hide(mCurrentFragment).show(fragment).commit();
        }
        mCurrentFragment = fragment;
    }



    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            int buttonViewId = buttonView.getId();
            if (buttonViewId == R.id.tab_home_btn) {
                changeFragment(mHomeFragment, TAG_HOME);
            } else if (buttonViewId == R.id.tab_message_btn) {
                changeFragment(mMessageFragment, TAG_MESSAGE);
            } else if (buttonViewId == R.id.tab_me_btn) {
                changeFragment(mMeFragment, TAG_ME);
            }
        }
    }
}
